//
//  RentHouseHeader.m
//  Liuhe
//
//  Created by zr on 2018/8/20.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "RentHouseHeader.h"

@interface RentHouseHeader ()

@property (nonatomic, strong)UIButton * oneButton;
@property (nonatomic, strong)UIButton * twoButton;
@property (nonatomic, strong)UIButton * threeButton;


@end


@implementation RentHouseHeader


- (void)setDict:(NSDictionary *)dict{
    
    _dict = dict;
    
    NSString * keyType;
    NSString * valueType;
    
    NSArray * allkeys = [dict allKeys];
//    NSLog(@"allkeys %@",allkeys);
    
    for (int i = 0; i < allkeys.count; i++)
    {
        keyType = [allkeys objectAtIndex:i];
        
        //如果你的字典中存储的多种不同的类型,那么最好用id类型去接受它
        valueType  = [dict objectForKey:keyType];
    }
    
    if ([keyType isEqualToString:@"one"]) {
        
        [self.oneButton setTitle:valueType forState:UIControlStateNormal];
    }else if ([keyType isEqualToString:@"two"]){
        
        [self.twoButton setTitle:valueType forState:UIControlStateNormal];
    }else if ([keyType isEqualToString:@"three"]){
        
        [self.threeButton setTitle:valueType forState:UIControlStateNormal];
    }
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = WHITE_COLOR;
    
    self.oneButton = [self setupButtonWith:@"区域"];
//    self.oneButton.selected = YES;
    [self addSubview:self.oneButton];
    
    self.twoButton = [self setupButtonWith:@"价格"];
    [self addSubview:self.twoButton];
    
    self.threeButton = [self setupButtonWith:@"房型"];
    [self addSubview:self.threeButton];
}


- (void)layoutSubviews{
    
    [self.oneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.leading.mas_equalTo(self).offset(20 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
        make.width.mas_equalTo(80 * SCREENH6);
    }];
    
    [self.twoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.oneButton);
        make.leading.mas_equalTo(self.oneButton.mas_trailing).offset(15 * SCREENH6);
    }];
    
    [self.threeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.height.width.mas_equalTo(self.twoButton);
        make.leading.mas_equalTo(self.twoButton.mas_trailing).offset(15 * SCREENH6);
    }];
}


- (UIButton *)setupButtonWith:(NSString *)str{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:str forState:UIControlStateNormal];
    [button setTitleColor:BLACK_COLOR forState:UIControlStateNormal];
    [button setTitleColor:MAINCOLOR forState:UIControlStateSelected];
    [button addTarget:self action:@selector(clickTopButton:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    return button;
}


- (void)clickTopButton:(UIButton *)button{
    
    NSString * type;
    
    if (button == self.oneButton) {
        
        type = @"one";
        if (!self.oneButton.selected) {
       
            self.oneButton.selected = YES;
            self.twoButton.selected = NO;
            self.threeButton.selected = NO;
        }
    }else if (button == self.twoButton){
        
        type = @"two";
        if (!self.twoButton.selected) {
         
            self.oneButton.selected = NO;
            self.twoButton.selected = YES;
            self.threeButton.selected = NO;
        }
    }else if (button == self.threeButton){
        
        type = @"three";
        if (!self.threeButton.selected) {
            
            self.oneButton.selected = NO;
            self.twoButton.selected = NO;
            self.threeButton.selected = YES;
        }
    }
    
    NSDictionary * dict = @{@"type":type};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showTopTableView" object:nil userInfo:dict];
}


@end
